﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using DBUtility;//Please add references
namespace DAL
{
	/// <summary>
	/// 数据访问类:User_Volunteer
	/// </summary>
	public partial class User_Volunteer
	{
		public User_Volunteer()
		{}
		#region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("Id", "User_Volunteer"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int Id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from User_Volunteer");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(Model.User_Volunteer model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into User_Volunteer(");
			strSql.Append("Email,Password,Nickname,Age,Sex,PhoneNumber,QQ,Img_Face,Area,Slogan,RoleId,IsEnable,LoginCount,LastIP,LastLoginTime,InsTime,IsDelete,IsFreeze,IsAuthentication,ApplyId)");
			strSql.Append(" values (");
			strSql.Append("@Email,@Password,@Nickname,@Age,@Sex,@PhoneNumber,@QQ,@Img_Face,@Area,@Slogan,@RoleId,@IsEnable,@LoginCount,@LastIP,@LastLoginTime,@InsTime,@IsDelete,@IsFreeze,@IsAuthentication,@ApplyId)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Nickname", SqlDbType.NVarChar,50),
					new SqlParameter("@Age", SqlDbType.Int,4),
					new SqlParameter("@Sex", SqlDbType.NVarChar,5),
					new SqlParameter("@PhoneNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,20),
					new SqlParameter("@Img_Face", SqlDbType.NVarChar,50),
					new SqlParameter("@Area", SqlDbType.NVarChar,50),
					new SqlParameter("@Slogan", SqlDbType.NVarChar,500),
					new SqlParameter("@RoleId", SqlDbType.Int,4),
					new SqlParameter("@IsEnable", SqlDbType.Bit,1),
					new SqlParameter("@LoginCount", SqlDbType.Int,4),
					new SqlParameter("@LastIP", SqlDbType.NVarChar,30),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@InsTime", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Bit,1),
					new SqlParameter("@IsFreeze", SqlDbType.Bit,1),
					new SqlParameter("@IsAuthentication", SqlDbType.Bit,1),
					new SqlParameter("@ApplyId", SqlDbType.Int,4)};
			parameters[0].Value = model.Email;
			parameters[1].Value = model.Password;
			parameters[2].Value = model.Nickname;
			parameters[3].Value = model.Age;
			parameters[4].Value = model.Sex;
			parameters[5].Value = model.PhoneNumber;
			parameters[6].Value = model.QQ;
			parameters[7].Value = model.Img_Face;
			parameters[8].Value = model.Area;
			parameters[9].Value = model.Slogan;
			parameters[10].Value = model.RoleId;
			parameters[11].Value = model.IsEnable;
			parameters[12].Value = model.LoginCount;
			parameters[13].Value = model.LastIP;
			parameters[14].Value = model.LastLoginTime;
			parameters[15].Value = model.InsTime;
			parameters[16].Value = model.IsDelete;
			parameters[17].Value = model.IsFreeze;
			parameters[18].Value = model.IsAuthentication;
			parameters[19].Value = model.ApplyId;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(Model.User_Volunteer model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update User_Volunteer set ");
			strSql.Append("Email=@Email,");
			strSql.Append("Password=@Password,");
			strSql.Append("Nickname=@Nickname,");
			strSql.Append("Age=@Age,");
			strSql.Append("Sex=@Sex,");
			strSql.Append("PhoneNumber=@PhoneNumber,");
			strSql.Append("QQ=@QQ,");
			strSql.Append("Img_Face=@Img_Face,");
			strSql.Append("Area=@Area,");
			strSql.Append("Slogan=@Slogan,");
			strSql.Append("RoleId=@RoleId,");
			strSql.Append("IsEnable=@IsEnable,");
			strSql.Append("LoginCount=@LoginCount,");
			strSql.Append("LastIP=@LastIP,");
			strSql.Append("LastLoginTime=@LastLoginTime,");
			strSql.Append("InsTime=@InsTime,");
			strSql.Append("IsDelete=@IsDelete,");
			strSql.Append("IsFreeze=@IsFreeze,");
			strSql.Append("IsAuthentication=@IsAuthentication,");
			strSql.Append("ApplyId=@ApplyId");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Email", SqlDbType.NVarChar,50),
					new SqlParameter("@Password", SqlDbType.NVarChar,50),
					new SqlParameter("@Nickname", SqlDbType.NVarChar,50),
					new SqlParameter("@Age", SqlDbType.Int,4),
					new SqlParameter("@Sex", SqlDbType.NVarChar,5),
					new SqlParameter("@PhoneNumber", SqlDbType.NVarChar,50),
					new SqlParameter("@QQ", SqlDbType.NVarChar,20),
					new SqlParameter("@Img_Face", SqlDbType.NVarChar,50),
					new SqlParameter("@Area", SqlDbType.NVarChar,50),
					new SqlParameter("@Slogan", SqlDbType.NVarChar,500),
					new SqlParameter("@RoleId", SqlDbType.Int,4),
					new SqlParameter("@IsEnable", SqlDbType.Bit,1),
					new SqlParameter("@LoginCount", SqlDbType.Int,4),
					new SqlParameter("@LastIP", SqlDbType.NVarChar,30),
					new SqlParameter("@LastLoginTime", SqlDbType.DateTime),
					new SqlParameter("@InsTime", SqlDbType.DateTime),
					new SqlParameter("@IsDelete", SqlDbType.Bit,1),
					new SqlParameter("@IsFreeze", SqlDbType.Bit,1),
					new SqlParameter("@IsAuthentication", SqlDbType.Bit,1),
					new SqlParameter("@ApplyId", SqlDbType.Int,4),
					new SqlParameter("@Id", SqlDbType.Int,4)};
			parameters[0].Value = model.Email;
			parameters[1].Value = model.Password;
			parameters[2].Value = model.Nickname;
			parameters[3].Value = model.Age;
			parameters[4].Value = model.Sex;
			parameters[5].Value = model.PhoneNumber;
			parameters[6].Value = model.QQ;
			parameters[7].Value = model.Img_Face;
			parameters[8].Value = model.Area;
			parameters[9].Value = model.Slogan;
			parameters[10].Value = model.RoleId;
			parameters[11].Value = model.IsEnable;
			parameters[12].Value = model.LoginCount;
			parameters[13].Value = model.LastIP;
			parameters[14].Value = model.LastLoginTime;
			parameters[15].Value = model.InsTime;
			parameters[16].Value = model.IsDelete;
			parameters[17].Value = model.IsFreeze;
			parameters[18].Value = model.IsAuthentication;
			parameters[19].Value = model.ApplyId;
			parameters[20].Value = model.Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from User_Volunteer ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string Idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from User_Volunteer ");
			strSql.Append(" where Id in ("+Idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public Model.User_Volunteer GetModel(int Id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 Id,Email,Password,Nickname,Age,Sex,PhoneNumber,QQ,Img_Face,Area,Slogan,RoleId,IsEnable,LoginCount,LastIP,LastLoginTime,InsTime,IsDelete,IsFreeze,IsAuthentication,ApplyId from User_Volunteer ");
			strSql.Append(" where Id=@Id");
			SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)
			};
			parameters[0].Value = Id;

			Model.User_Volunteer model=new Model.User_Volunteer();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["Id"]!=null && ds.Tables[0].Rows[0]["Id"].ToString()!="")
				{
					model.Id=int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Email"]!=null && ds.Tables[0].Rows[0]["Email"].ToString()!="")
				{
					model.Email=ds.Tables[0].Rows[0]["Email"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Password"]!=null && ds.Tables[0].Rows[0]["Password"].ToString()!="")
				{
					model.Password=ds.Tables[0].Rows[0]["Password"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Nickname"]!=null && ds.Tables[0].Rows[0]["Nickname"].ToString()!="")
				{
					model.Nickname=ds.Tables[0].Rows[0]["Nickname"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Age"]!=null && ds.Tables[0].Rows[0]["Age"].ToString()!="")
				{
					model.Age=int.Parse(ds.Tables[0].Rows[0]["Age"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Sex"]!=null && ds.Tables[0].Rows[0]["Sex"].ToString()!="")
				{
					model.Sex=ds.Tables[0].Rows[0]["Sex"].ToString();
				}
				if(ds.Tables[0].Rows[0]["PhoneNumber"]!=null && ds.Tables[0].Rows[0]["PhoneNumber"].ToString()!="")
				{
					model.PhoneNumber=ds.Tables[0].Rows[0]["PhoneNumber"].ToString();
				}
				if(ds.Tables[0].Rows[0]["QQ"]!=null && ds.Tables[0].Rows[0]["QQ"].ToString()!="")
				{
					model.QQ=ds.Tables[0].Rows[0]["QQ"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Img_Face"]!=null && ds.Tables[0].Rows[0]["Img_Face"].ToString()!="")
				{
					model.Img_Face=ds.Tables[0].Rows[0]["Img_Face"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Area"]!=null && ds.Tables[0].Rows[0]["Area"].ToString()!="")
				{
					model.Area=ds.Tables[0].Rows[0]["Area"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Slogan"]!=null && ds.Tables[0].Rows[0]["Slogan"].ToString()!="")
				{
					model.Slogan=ds.Tables[0].Rows[0]["Slogan"].ToString();
				}
				if(ds.Tables[0].Rows[0]["RoleId"]!=null && ds.Tables[0].Rows[0]["RoleId"].ToString()!="")
				{
					model.RoleId=int.Parse(ds.Tables[0].Rows[0]["RoleId"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsEnable"]!=null && ds.Tables[0].Rows[0]["IsEnable"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsEnable"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsEnable"].ToString().ToLower()=="true"))
					{
						model.IsEnable=true;
					}
					else
					{
						model.IsEnable=false;
					}
				}
				if(ds.Tables[0].Rows[0]["LoginCount"]!=null && ds.Tables[0].Rows[0]["LoginCount"].ToString()!="")
				{
					model.LoginCount=int.Parse(ds.Tables[0].Rows[0]["LoginCount"].ToString());
				}
				if(ds.Tables[0].Rows[0]["LastIP"]!=null && ds.Tables[0].Rows[0]["LastIP"].ToString()!="")
				{
					model.LastIP=ds.Tables[0].Rows[0]["LastIP"].ToString();
				}
				if(ds.Tables[0].Rows[0]["LastLoginTime"]!=null && ds.Tables[0].Rows[0]["LastLoginTime"].ToString()!="")
				{
					model.LastLoginTime=DateTime.Parse(ds.Tables[0].Rows[0]["LastLoginTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["InsTime"]!=null && ds.Tables[0].Rows[0]["InsTime"].ToString()!="")
				{
					model.InsTime=DateTime.Parse(ds.Tables[0].Rows[0]["InsTime"].ToString());
				}
				if(ds.Tables[0].Rows[0]["IsDelete"]!=null && ds.Tables[0].Rows[0]["IsDelete"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsDelete"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsDelete"].ToString().ToLower()=="true"))
					{
						model.IsDelete=true;
					}
					else
					{
						model.IsDelete=false;
					}
				}
				if(ds.Tables[0].Rows[0]["IsFreeze"]!=null && ds.Tables[0].Rows[0]["IsFreeze"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsFreeze"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsFreeze"].ToString().ToLower()=="true"))
					{
						model.IsFreeze=true;
					}
					else
					{
						model.IsFreeze=false;
					}
				}
				if(ds.Tables[0].Rows[0]["IsAuthentication"]!=null && ds.Tables[0].Rows[0]["IsAuthentication"].ToString()!="")
				{
					if((ds.Tables[0].Rows[0]["IsAuthentication"].ToString()=="1")||(ds.Tables[0].Rows[0]["IsAuthentication"].ToString().ToLower()=="true"))
					{
						model.IsAuthentication=true;
					}
					else
					{
						model.IsAuthentication=false;
					}
				}
				if(ds.Tables[0].Rows[0]["ApplyId"]!=null && ds.Tables[0].Rows[0]["ApplyId"].ToString()!="")
				{
					model.ApplyId=int.Parse(ds.Tables[0].Rows[0]["ApplyId"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select Id,Email,Password,Nickname,Age,Sex,PhoneNumber,QQ,Img_Face,Area,Slogan,RoleId,IsEnable,LoginCount,LastIP,LastLoginTime,InsTime,IsDelete,IsFreeze,IsAuthentication,ApplyId ");
			strSql.Append(" FROM User_Volunteer ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" Id,Email,Password,Nickname,Age,Sex,PhoneNumber,QQ,Img_Face,Area,Slogan,RoleId,IsEnable,LoginCount,LastIP,LastLoginTime,InsTime,IsDelete,IsFreeze,IsAuthentication,ApplyId ");
			strSql.Append(" FROM User_Volunteer ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM User_Volunteer ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.Id desc");
			}
			strSql.Append(")AS Row, T.*  from User_Volunteer T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "User_Volunteer";
			parameters[1].Value = "Id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

